import 'package:cows_bulls_game/model/screen_dimensions.dart';
import 'package:cows_bulls_game/ui/instructions_screen/instructions_screen.dart';
import 'package:cows_bulls_game/ui/main_screen/consts/main_screen_colors.dart';
import 'package:cows_bulls_game/ui/main_screen/consts/main_screen_images.dart';
import 'package:cows_bulls_game/ui/main_screen/consts/main_screen_text_styles.dart';
import 'package:cows_bulls_game/ui/main_screen/main_screen_button.dart';
import 'package:cows_bulls_game/ui/pve_game/pve_game_initializer.dart';
import 'package:cows_bulls_game/ui/single_player_game/single_game_initializer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';

class MainScreenWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ScreenDimensions dimensions = ScreenDimensions(context);
    return Scaffold(
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.light,
        child: Container(
          width: dimensions.width,
          height: dimensions.fullHeight,
          color: mainScreenBackColor,
          child: Stack(children: [
            _buildBackgroundImage(dimensions),
            _buildMainScreenImage(dimensions),
            _buildMainScreenTitle(dimensions),
            Row(children: [
              Expanded(
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      GestureDetector(
                          onTap: () => _startSinglePlayerGame(context),
                          child: MainScreenButton("单人模式", dimensions,
                              playButtonDecoration, playButtonTextStyle)),
                      SizedBox(height: dimensions.withoutSafeAreaHeight * 0.01),
                      GestureDetector(
                          onTap: () => _startPveGame(context),
                          child: MainScreenButton("A I 对 战", dimensions,
                              playButtonDecoration, playButtonTextStyle)),
                      SizedBox(height: dimensions.withoutSafeAreaHeight * 0.01),
                      GestureDetector(
                          onTap: () => _openInstructionsScreen(context),
                          child: MainScreenButton("游戏规则", dimensions,
                              rulesButtonDecoration, rulesButtonTextStyle)),
                      SizedBox(height: dimensions.withoutSafeAreaHeight * 0.1)
                    ]),
              )
            ])
          ]),
        ),
      ),
    );
  }

  Widget _buildBackgroundImage(ScreenDimensions dimensions) {
    print(dimensions.width);
    return SvgPicture.asset(
      backElementsImage,
      width: dimensions.width,
    );
  }

  Widget _buildMainScreenImage(ScreenDimensions dimensions) {
    return Center(
      child: Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Image.asset(cowAndBullImage, height: dimensions.fullHeight * 0.4,),
          ]),
    );
  }

  Widget _buildMainScreenTitle(ScreenDimensions dimensions) {
    return Column(children: [
      SizedBox(height: dimensions.withoutSafeAreaHeight * 0.06),
      Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        Container(
            width: dimensions.width * 0.7,
            height: dimensions.fullHeight * 0.3,
            child: Center(
                child: Stack(
              children: <Widget>[
                Text(
                  'Bingo数字解密',
                  style: TextStyle(
                    fontSize: 40,
                    foreground: Paint()
                      ..style = PaintingStyle.stroke
                      ..strokeWidth = 16
                      ..color = Color.fromARGB(255, 75, 30, 7),
                  ),
                ),
                // Solid text as fill.
                Text(
                  'Bingo数字解密',
                  style: TextStyle(
                    fontSize: 40,
                    color: Color.fromARGB(255, 220, 74, 16),
                  ),
                ),
              ],
            )))
      ]),
    ]);
  }

  void _startSinglePlayerGame(BuildContext context) {
    Navigator.push(context, MaterialPageRoute(builder: (routeContext) {
      return SingleGameInitializer();
    }));
  }

  void _startPveGame(BuildContext context) {
    Navigator.push(context, MaterialPageRoute(builder: (routeContext) {
      return PveGameInitializer();
    }));
  }

  void _openInstructionsScreen(BuildContext context) {
    Navigator.push(context, MaterialPageRoute(builder: (routeContext) {
      return InstructionsScreen();
    }));
  }
}
